package com.msh.automatic.android.control.script.util

import android.app.Activity
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.Toast
import kotlinx.coroutines.delay
import kotlin.system.exitProcess

object TaskUtil {

    fun callBack(taskLogId:String?, message:String, context: Context):Intent{
        if(!message.isNullOrEmpty()){
            Handler(Looper.getMainLooper()).post {
                Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
            }
        }

        val launchIntent = Intent().apply {
            component = ComponentName(
                "com.msh.automatic.android.control",  // 目标包名
                "com.msh.automatic.android.control.MainActivity"  // 目标Activity类名
            )
            putExtra("callPackage", context.packageName)
            putExtra("callClass", context::class.java.name)
            putExtra("message", message)
            putExtra("taskLogId", taskLogId)
            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        }
        return launchIntent
    }
    fun callBackActivity(taskLogId:String?, message:String, context: Context){
        context.startActivity(callBack(taskLogId,message,context))
        (context as? Activity)?.finish()
        exitProcess(0)
    }
}